<?php
/**
 * Array holding DateTime Objects
 */
$week = array();

/**
 * Flag determines if we have
 * a problem week or not
 */

//$problemW = false;

//$date = new DateTime("2012-01-15");
$date = new DateTime("today");


$startD = strtolower($date->format('D'));


/*if ( $startD == 'sun') {
    $problemW = true;
}*/

print "<pre>";
	var_dump($date);
print "</pre>";

$dateperiod = getPeriod($date);

/*echo "<h1>Problem Week</h1>";
foreach($dateperiod as $day) {
	echo $day->format('D, M d') . "<br />";
}*/
/*
if ($problemW) {
    $week = fixWeek($dateperiod);
}*/

$week = fixWeek($dateperiod);

echo "<h1>Fixed Week</h1>";

foreach($week as $day) {
	echo $day . "<br />";
}

function getPeriod(DateTime $date, $days = 6)
{
	$wk = new DatePeriod($date, new DateInterval('P1D'), $days);
	return $wk;
}

/**
 * This function will reorder the week days
 * so that saturday is always before sunday
 */
function fixweek($dp)
{
    $days = array();
    $tmp;

    foreach ($dp as $day) {
        if (strtolower($day->format('D')) == 'sat') {
            $tmp = $day->format('D, M d');   
        } else {
            $days[] = $day->format('D, M d');
        }
    }

    array_unshift($days, $tmp);

    return $days;

}